﻿@model EditThemeViewModel
@{
	ViewBag.Title = String.Format("Edit theme {0}", Model.Name);
}

<h2>@ViewBag.Title</h2>

@using (Html.BeginForm("Update", "Themes", FormMethod.Post)) {
	@Html.AntiForgeryToken()
	@Html.HiddenFor(m => m.Name)
	<fieldset>
		<legend>Theme properties</legend>
		<div>
			@Html.LabelFor(m => m.Description, "Description")
			@Html.TextBoxFor(m => m.Description, new { @class = "te fe-wide" })
		</div>
		<div>
			@Html.LabelFor(m => m.ContentRegions, "Content regions")
			@Html.TextBoxFor(m => m.ContentRegions, new { @class = "te fe-wide" })
		</div>
	</fieldset>
	<p>
		<input type="submit" value="Update theme properties" />
		or @Html.ActionLink("Cancel", "Index")
	</p>
}

<h2>File editor</h2>

@using (Html.BeginForm("UpdateFile", "Themes", FormMethod.Post, new { id = "fileform" })) {
	@Html.AntiForgeryToken()
	<input type="hidden" name="virtualfilepath" id="virtualfilepath" value="" />
	<fieldset>
		<legend></legend>
		<textarea id="themeeditor" name="filecontent" class="code te fe-max" style="height:350px"></textarea>
	</fieldset>
	<p>
		<input type="submit" value="Save file" id="savefilebutton" disabled="disabled" />
		<span id="fileineditor"></span>
	</p>
}

<script type="text/javascript">
	$(document).ready(function () {
	    $('.editfilelink').click(function () {
			var path = $(this).attr('href');
			var url = "@Url.Action("EditFile")?virtualFilePath=" + path;

			$.get(url, function(data) {
				$('#themeeditor').val(data.Content);
				$('#virtualfilepath').val(data.File);
				$('#fileineditor').html(data.File);
				if (data.CanWrite) {
					$('#savefilebutton').removeAttr("disabled");
				} else {
					$('#savefilebutton').attr("disabled", "disabled");
				}
			});
			
			return false;
		});

		$('#fileform').submit(function() {
			var url = $(this).attr('action');
			var data = $(this).serialize();
			$.post(url, data);
			return false;
		});
	});
</script>

@section head {
	@Cms.ScriptInclude("Themes/Admin/Scripts/codemirror/codemirror.js")
	@Cms.CssLink("Themes/Admin/Scripts/codemirror/codemirror.css")
	@Cms.CssLink("Themes/Admin/Scripts/codemirror/default.css")
}

@section sidebar {
	<h3>Theme files</h3>
	<ul>
		@DisplayThemeDirectory(Model.Styles)
		@DisplayThemeDirectory(Model.Views)
	</ul>
}

@helper DisplayThemeDirectory(ThemeDirectory themeDirectory) {
	<li>
		<strong>@themeDirectory.Name</strong>
		<ul>
			@foreach(var subDirectory in themeDirectory.SubDirectories) {
				@DisplayThemeDirectory(subDirectory)
			}
		</ul>
		<ul>
			@foreach(var file in themeDirectory.Files) {
				var filePath = Cms.Content("Themes/" + Model.Name + "/" + themeDirectory.Path + "/" + file);
				<li><a class="editfilelink" href="@filePath">@file</a></li>
		}
		</ul>
	</li>
}

